package com.vitaf.cash.app.ui.fragment

import android.view.LayoutInflater
import android.view.View
import androidx.core.graphics.toColorInt
import androidx.fragment.app.Fragment
import com.vitaf.cash.app.R
import com.vitaf.cash.app.common.VitafComFmtV2
import com.vitaf.cash.app.databinding.FragmentHomeBinding
import com.vitaf.cash.app.event.VitafEventData
import com.vitaf.cash.app.utils.helper.VitafProcessHp

/**
 * 首页 额度、审核中、放款中、已拒绝、待还款
 */
class TabHomeFragment: VitafComFmtV2() {
    private var _binding: FragmentHomeBinding? = null

    private var mainHomeFragment: HomeLimitFragment? = null
    //审核中、放款中、已拒绝
    private var mainStatusFragment: VitafStatusFmt? = null
    //待还款
    private var mainRepayFragment: VitafRepayFmt? = null

    override var pageName: String = "Home_Page"

    override val enablePageTrack: Boolean
        get() = false

    override fun createVitafContentView(inflater: LayoutInflater): View? {
        _binding = FragmentHomeBinding.inflate(inflater)
        return _binding?.root
    }

    override fun initializeVitafViews() {
        // 设置 DataBinding
        _binding?.homeFragment = this
        _binding?.lifecycleOwner = viewLifecycleOwner
        setViewPaddingTopStatusBarHeight(_binding?.homeRootLayout)
    }

    override fun setupVitafEventListeners() {
    }

    override fun loadVitafData() {
    }

    /**
     * 根据状态显示或刷新对应Fragment
     */
    private fun checkRefreshDataByApplyStatus(){
        //待还款
        val isRepay = VitafProcessHp.isGombeCreditRepay()
        if (isRepay){
            showApplyRepayFragment()
            return
        }
        //审核中 || 放款中 || 已拒绝
        val isReviewing = VitafProcessHp.isGombeCreditReviewing()
        val isDisbursing = VitafProcessHp.isGombeCreditDisbursing()
        val isRejected = VitafProcessHp.isGombeCreditRejected()
        if (isReviewing or isDisbursing or isRejected){
            showApplyStatusFragment()
            return
        }
        showApplyLimitFragment()
    }

    /**
     * 首页额度
     */
    private fun showApplyLimitFragment(){
        _binding?.homeRootLayout?.setBackgroundResource(R.drawable.vitaf_root_bg)
        mainStatusFragment = null
        mainRepayFragment = null
        if (mainHomeFragment == null){
            mainHomeFragment = HomeLimitFragment()
            pageName = mainHomeFragment?.pageName.toString()
            showChildFragmentPage(mainHomeFragment)
        } else {//已经添加过，需要调用Fragment刷新方法

        }
    }

    /**
     * 审核中、放款中、已拒绝
     */
    private fun showApplyStatusFragment(){
        _binding?.homeRootLayout?.setBackgroundColor("#ffffffff".toColorInt())
        mainHomeFragment = null
        mainRepayFragment = null
        if (mainStatusFragment == null){
            mainStatusFragment = VitafStatusFmt()
            pageName = mainStatusFragment?.pageName.toString()
            showChildFragmentPage(mainStatusFragment)
        } else {//已经添加过，需要调用Fragment刷新方法

        }
    }

    /**
     * 待还款
     */
    private fun showApplyRepayFragment(){
        _binding?.homeRootLayout?.setBackgroundResource(R.drawable.vitaf_root_bg)
        mainHomeFragment = null
        mainStatusFragment = null
        if (mainRepayFragment == null){
            mainRepayFragment = VitafRepayFmt()
            pageName = mainRepayFragment?.pageName.toString()
            showChildFragmentPage(mainRepayFragment)
        } else {//已经添加过，需要调用Fragment刷新方法

        }
    }

    private fun showChildFragmentPage(fragment: Fragment?){
        fragment?.let {
            childFragmentManager.beginTransaction()
                .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
                .replace(R.id.home_content_layout,fragment)
                .commitAllowingStateLoss()
        }
    }

    override fun onVitafPublishEvent(event: VitafEventData) {
        super.onVitafPublishEvent(event)
        when(event.eventType){
            VitafEventData.TYPE.GOMBECREDIT_PROCESS_REFRESHED -> {//流程刷新完成
                checkRefreshDataByApplyStatus()
            }
            else -> {

            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mainHomeFragment = null
        mainStatusFragment = null
        mainRepayFragment = null
        _binding = null
    }
}